<?php
class Add extends Controller {
	public function html() {
		$mUser = new User_model();
		$currUser = $mUser->currUser();
		if(!$currUser) header('Location:/auth/login?backurl='.urlencode('/rbac/node/add'));
		
		$data = array();
		$tpl = 'rbac/node/nodeinfo.html';
		
		//获取所有父分类
		$mNode = new Node_model();
		$result = $mNode->groupAll();
		$data['groups'] = array();
		if(is_array($result))
		foreach ($result as $key => $value) {
			if($value['pid']) {
				$data['groups'][$value['pid']]['sub'][$value['gid']] = $value['name'];
			} else {
				$data['groups'][$value['gid']]['name'] = $value['name'];
			}
		}
		
		$this->display($data, $tpl);
	}
	public function json() {
		$name = $this->post('name', 'str');
		$gid = $this->post('gid', 'int');
		$target = $this->post('target', 'str');
		$isMenu = $this->post('isMenu', 'int');
		$controller = $this->post('controller', 'str');
		
		if(!$name) $this->displayJSON(array('status' => -1, 'message' => '请输入节点名称'));
		if(!$gid) $this->displayJSON(array('status' => -2, 'message' => '请选择节点分组'));
		if(!$target) $this->displayJSON(array('status' => -3, 'message' => '请选择节点标识'));
		if($isMenu && !$controller) $this->displayJSON(array('status' => -4, 'message' => '请输入控制'));
		
		$mNode = new Node_model();
		
		//检查节点名称是否存在
		$params = array('name' => $name, 'gid' => $gid);
		$result = $mNode->nodeInfo($params);
		if($result) $this->displayJSON(array('status' => -5, 'message' => '节点名称已存在'));
		
		//检查节点标识是否存在
		$params = array('target' => $target);
		$result = $mNode->nodeInfo($params);
		if($result) $this->displayJSON(array('status' => -6, 'message' => '节点标识已存在'));
		
		$params = array('name' => $name, 'gid' => $gid, 'target' => $target, 'is_menu' => $isMenu, 'controller' => $controller);
		$result = $mNode->create($params);
		if($result) $this->displayJSON(array('status' => 0, 'message' => '添加节点成功'));
		else $this->displayJSON(array('status' => -7, 'message' => '添加节点失败'));
	}
}